﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Ccit.OA.NewsCenter
{
    public partial class news_search_tree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                _hidTabId.Value = ( null != Request.QueryString["TABID"]) ? Request.QueryString["TABID"].ToString() : "";
                _hidWhereClause.Value = Ccit.OA.Utils.SysInterface.GetPageDataRangeSQL("NEWS_A2", _hidTabId.Value);

                _hidType.Value = (null != Request.QueryString["Type"]) ? Request.QueryString["Type"].ToString() : "0";               
                initializePage();
            }
        }
        #region private Methods
        private void initializePage()
        {
            createTree();
            tvColumn.ExpandLevel = 1;
        }

        private void createTree()
        {
            //创建树
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            createRootNode(ref oRoot);
            tvColumn.Nodes.Add(oRoot);
            createSubNodes(ref oRoot);
        }
        //创建跟节点
        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            oRoot.Text = "信息栏目";
            oRoot.ImageUrl = "../images/toolbar/Folder_open.gif";

            string sPageParam = "Type="+_hidType.Value+"&columnId=";
            string encodeParam = base64encode(sPageParam);

            oRoot.NavigateUrl = "./news_search_list.aspx?" + encodeParam;
            oRoot.Target = "iFrmList";
            oRoot.ID = "0_0";
        }
        private string base64encode(string sPageParam)
        {
            string encodeParam = string.Empty;
            if (string.IsNullOrEmpty(sPageParam)) return encodeParam;

            encodeParam = Base64Lib.Encode(sPageParam);
            return encodeParam;
        }
        //创建子节点
        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            ColumnBLL bll = new ColumnBLL();
            string hasChild = "";
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;

            List<MsColumn> mclist = bll.GetColListByPurview(_hidType.Value);

            string PurColIds = string.Empty;
            foreach (MsColumn col in mclist)
            {
                PurColIds += (PurColIds == "") ? col.columnId : "," + col.columnId;
            }

            _hidPurColIds.Value = PurColIds;

            List<MsColumn> lstAll = bll.GetTreeData(_hidType.Value);

            foreach (MsColumn mc in lstAll)
            {
                if (mc.columnLevelCode.Length == 4)
                {
                    bool cFind = false;
            
                    string sChildColIds = bll.GetAllChildColId(mc.columnLevelCode);
                    if (sChildColIds != "")
                    {
                        string[] childIdArry = sChildColIds.Split(',');
                        for (int i = 0; i < childIdArry.Length; i++)  //判断其下面子节点有没有浏览权限
                        {
                            if (PurColIds.Contains(childIdArry[i]))
                            {
                                cFind = true;
                            }
                        }
                    }
                    
                    bool fFind = PurColIds.Contains(mc.columnId);
                    if (fFind ==true || cFind == true)
                    {
                        hasChild = (bll.isHasChildType(mc.columnLevelCode)) ? "1" : "0";
                        oTreeNode = createTreeNode(mc.columnName, mc.columnId, hasChild, fFind, cFind);
                        oRoot.Nodes.Add(oTreeNode);
                    }
                }
            }
        }

        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string columnName, string columnId, string hasChild,bool fFind,bool cFind)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();

            if (fFind == true)
            {
                oNode.Text = columnName;
                string sPageParam = "Type=" + _hidType.Value + "&columnId=" + columnId;
                string encodeParam = base64encode(sPageParam);
                oNode.NavigateUrl = "./news_search_list.aspx?" + encodeParam;
            }
            else
            {
                oNode.Text = columnName;
                oNode.NavigateUrl = "";
                oNode.DefaultStyle["color"] = "red";
            }

            oNode.ImageUrl = "../images/toolbar/filelogo.gif";
            oNode.Target = "iFrmList";
            oNode.ID = columnId + "_" + "0";
            oNode.Type = "root";

            if (hasChild == "1" && cFind == true)
            {
                Microsoft.Web.UI.WebControls.TreeNode oSubNode = new Microsoft.Web.UI.WebControls.TreeNode();
                oNode.Nodes.Add(oSubNode);
            }
            return oNode;
        }

        #endregion

        #region Ajax Methods
        /// <summary>
        /// get all sub asset types
        /// </summary>
        /// <param name="astTypeId"></param>
        /// <param name="nodeIndex"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string GetSubTypesByColumnId(string columnId, string nodeIndex)
        {
            ColumnBLL bll = new ColumnBLL();
            List<MsColumn> list = bll.GetSubColumnTypes(columnId);
            string hasChild = "";
            List<MsColumn> rlist = new List<MsColumn>();
            foreach (MsColumn mc in list)
            {
                hasChild = (bll.isHasChildType(mc.columnLevelCode)) ? "1" : "0";
                mc.hasChild = hasChild;
                mc.ChildColIds = bll.GetAllChildColId(mc.columnLevelCode);
                rlist.Add(mc);
            }
            string strResult = JavaScriptConvert.SerializeObject(rlist) + "_" + nodeIndex + "_" + columnId;
            return strResult;
        }

        #endregion
    }
}
